{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="p-3">
    <form class="layui-form gg-form-bar border-x" lay-filter="barsearchform" id="barsearchform">
        <div class="layui-input-inline" style="width:100px;">
            <input type="text" class="layui-input"  placeholder="ID"  name="id">
        </div>
        <div class="layui-input-inline" style="width:400px;">
            <input type="text" class="layui-input"  placeholder="联系人/电话/职位/计划/线索内容"  name="text">
        </div>
        <div class="layui-input-inline" style="width:100px;">
            <input type="text" class="layui-input"  placeholder="负责人"  name="fzName">
        </div>
        <div class="layui-input-inline" style="width:100px;">
            <input type="text" class="layui-input"  placeholder="创建人"  name="createName">
        </div>
        <div class="layui-input-inline" style="width:300px;">
            <input type="text" class="layui-input" id="apply_time" placeholder="沟通时间" readonly name="apply_time">
        </div>
        <div class="layui-input-inline" style="width:150px;">
            <select name="status" >
                <option value="">状态</option>
                <option value="2">已转客户</option>
                <option value="1">未转客户</option>
            </select>
        </div>
        <div class="layui-input-inline" style="width:150px;">
            <select name="fromType" >
                <option value="">来源</option>
                <option value="1">自己创建</option>
                <option value="2">转入</option>
            </select>
        </div>
        <div class="layui-input-inline" style="width:150px;">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
            <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
        </div>

    </form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
    <a href="javascript:" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加</a>
    	<a href="javascript:" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="dels">删除</a>
    	<a href="javascript:" class="layui-btn layui-btn-warm layui-btn-sm" lay-event="toPur">转成客户</a>
    	<a href="javascript:" class="layui-btn  layui-btn-sm" lay-event="toOther">转给他人</a>
    	<a href="{:url('api/exportModel/nicheClueExportModel')}" class="layui-btn  layui-btn-sm layui-btn-primary" >导入模板</a>
    	<a href="javascript:" class="layui-btn  layui-btn-sm layui-btn-normal" lay-event="import" >导入</a>
</script>
<a class="layui-btn layui-btn-sm layui-btn-primary" id="import" style="display: none" >导入</a>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool','employeepicker','laydatePlus'];
    function gouguInit() {
        var form = layui.form,table = layui.table, tool = layui.tool ,element=layui.element,laydatePlus = layui.laydatePlus;
        var upload=layui.upload;
        //日期范围
        var apply_time = new laydatePlus({'target':'apply_time'});

        layui.laydate.render({
            elem:"#bTime"
        })
        layui.laydate.render({
            elem:"#eTime"
        })
        element.on('tab(tab)', function(data){
            $("#barsearchform")[0].reset();
            layui.pageTable.reload({where:{status:data.index},page:{curr:1}});
            $('[name="status"]').val(data.index);
            return false;
        });

        //监听搜索提交
        form.on('submit(webform)', function(data) {
            layui.pageTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });
        $('[lay-filter="clear"]').on('click',function(){
            setTimeout(function(){
                $('[lay-filter="webform"]').click();
            },10)
        });

        layui.pageTable = table.render({
            elem: '#test'
            ,toolbar: '#toolbarDemo'
            ,defaultToolbar: false
            ,title:'商机线索'
            ,url: "{:url('index')}"
            ,page: true //开启分页
            ,limit: 20
            ,cellMinWidth: 80
            ,cols: [[
                {type: 'checkbox', fixed: 'left' },
                {field:'id',width:80, title: 'ID号', align:'center'}
                ,{field:'punterName',title: '客户名称',align:'center'}
                ,{field:'contactName',title: '线索联系人姓名',align:'center'}
                ,{field:'contactPhone',title: '线索联系人电话',align:'center'}
                ,{field:'contactJob',title: '线索联系人职位',align:'center'}
                // ,{field:'gTime',title: '沟通时间',align:'center'}
                ,{field:'content',title: '线索内容',align:'center'}
                // ,{field:'applane',title: '后续跟进计划'}
                ,{field:'fzName',title: '负责人'}
                ,{field:'createUser',title: '创建人'}
                ,{field:'status',title: '状态',templet:function (d) {
                    return d.status==1?'未转客户':'已转客户';
                    }}
                ,{field:'create_time',title: '创建时间',templet:function (d) {
                    return formatTimeTwo(d.create_time)
                    }}
                ,{width:120,fixed: 'right',title: '操作', align:'center',templet: function(d){
                	var btn='<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>' +
                        '<a class="layui-btn layui-btn-xs" lay-event="info">详情</a>';
                	return btn;
                }}
            ]]
        });

        table.on('tool(test)',function (obj) {
        	if(obj.event === 'edit'){
        		tool.side("{:url('add')}?id="+obj.data.id);
        		return;
        	}
            if(obj.event === 'info'){
                tool.side("{:url('info')}?id="+obj.data.id);
                return;
            }
        });
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    tool.side("{:url('add')}");
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                if (e.code == 0) {
                                    layui.pageTable.reload();
                                }
                            }
                            httpRequest("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'toPur':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要转客户吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                if (e.code == 0) {
                                    layui.pageTable.reload();
                                }
                            }
                            httpRequest("{:url('toPurveyor')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'toOther':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })

                        layer.open({
                            type: 1,
                            title: '选择用户',
                            area:['300px','300px'],
                            btn:['确定'],
                            content: '<form class="layui-form" id="printForm" action="">' +
                                '  <div class="layui-form-item" >' +
                                // '    <div class="layui-input-block">' +
                                '       <select name="uid" id="toUid" lay-event="toUid" lay-search><option value="">选择用户</option>' +
                                '{volist name="adminList" id="al"}<option value="{$al.id}">{$al.name}</option>{/volist}' +
                                '</select>' +
                                // '    </div>' +
                                '  </div>' +
                                '</form>',
                            success: function(layero, index){
                                // 渲染表单
                                form.render();
                                //
                                // // 监听提交事件
                                // form.on('radio(printType)', function(data){
                                //     console.log(data.value); // 获取选中的值
                                //     layer.close(index); // 关闭弹窗
                                // });
                            },
                            yes:function (index) {
                                var form =document.getElementById('printForm');
                                let data=$(form).serialize();
                                httpRequest("{:url('toOther')}?id="+ids.join(',')+"&"+data,{},function (res) {
                                    if (res.code == 0) {
                                            layui.pageTable.reload();
                                    }
                                    layer.close(index);
                                })

                            }
                        });



                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':
                   $("#import").click()
                    break;
            };
        })

        upload.render({
            elem: '#import'
            ,url: "{:url('importData')}"
            ,accept: 'file'
            ,exts: 'xlsx',
            before:function () {
                layui.layer.load()
            }
            ,done: function(res){
                layui.layer.closeAll()
                if (res.code!=0){
                    layer.msg(res.msg,{icon:5})
                    return;
                }
                layer.msg(res.msg)
                setTimeout(function () {
                    location.reload()
                },2000)
            }
        });
    }
</script>
{/block}
<!-- /脚本 -->